package com.longcai.controller.homepage;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homepage.ClyjGoodsType;
import com.longcai.entity.homepage.ClyjUserHotsearch;
import com.longcai.entity.homepage.ClyjUserSearch;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.mapper.homegood.ClyjGoodsHomepageMapper;
import com.longcai.mapper.homepage.ClyjUserHotsearchMapper;
import com.longcai.mapper.homepage.GoodsClassMapper;
import com.longcai.service.homepage.ClyjGoodsTypeService;
import com.longcai.service.homepage.ClyjUserHotsearchService;
import com.longcai.service.homepage.ClyjUserSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Controller
@RequestMapping("/clyjUserSearch")
public class ClyjUserSearchController {
    @Autowired
    ClyjUserSearchService bannerService;
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;
    @Autowired
    private GoodsClassMapper goodsClassMapper;
    @Autowired
    private ClyjGoodsTypeService clyjGoodsTypeService;
    @Autowired
    private ClyjUserHotsearchService clyjUserHotsearchService;
    @Autowired
    private ClyjUserHotsearchMapper clyjUserHotsearchMapper;

    /**
     * description: 首页获取当前用户模糊查询历史
     * author: LWK
     * time： 14点29分
     */
    @RequestMapping("/getSearchList")
    @ResponseBody
    public ApiResult<List<ClyjUserSearch>> getSearchList(@RequestBody JSONObject params) {
        //获取用户id
        String userId = params.getString("userId");
        //0是首页 1是附近
        String flag = params.getString("flag");
        ApiResult<List<ClyjUserSearch>> result = new ApiResult<List<ClyjUserSearch>>();
        try {
            QueryWrapper<ClyjUserSearch> queryWrapper = new QueryWrapper<ClyjUserSearch>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("flag", Integer.parseInt(flag));
            queryWrapper.orderByDesc("search_time");
            result.success();
            result.setData(bannerService.selectList(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * description: 首页模糊查询
     * author: LWK
     * time： 14点53分
     */
    @RequestMapping("/getGoodsListBySearch")
    @ResponseBody
    public ApiResult<List<ClyjUserSearch>> getGoodsListBySearch(@RequestBody JSONObject params) {
        //获取关键词
        String name = params.getString("name");
        //获取用户id
        String userId = params.getString("userId");
        ApiResult<List<ClyjUserSearch>> result = new ApiResult<List<ClyjUserSearch>>();
        Map map = new HashMap();
        List arr = new ArrayList();
        List list = new ArrayList();
        try {
            //去查询类型
            QueryWrapper<GoodsClass> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.like("name", name);
            queryWrapper1.eq("type_status", "0");
            List<GoodsClass> typeList = this.goodsClassMapper.selectList(queryWrapper1);
            for (GoodsClass goodsClass : typeList) {
                list.add(goodsClass.getId());
            }
            map.put("list", list);
            //查询关联表里商品
            List<ClyjGoodsType> clyjGoodsTypes = this.clyjGoodsTypeService.getGoodsId(map);
            for (ClyjGoodsType clyjGoodsType : clyjGoodsTypes) {
                arr.add(clyjGoodsType.getGoodsId());

            }
            map.put("arr", arr);
            map.put("name", name);
            //数据库查询商品
            List<ClyjGoodsHomepage> list1 = this.clyjGoodsHomepageMapper.selectall(map);

            //存储热门搜索
            this.clyjUserHotsearchService.insertsearch(name);

            //存储用户搜索记录
            QueryWrapper<ClyjUserSearch> queryWrapper = new QueryWrapper<ClyjUserSearch>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("search_name", name);
            queryWrapper.orderByDesc("search_time");
            List<ClyjUserSearch> selectList = bannerService.selectList(queryWrapper);
            //查询该记录是否已经存在
            if (selectList != null && selectList.size() > 0) {
                return ApiResult.success(list1);
            }
            //不存在则插入数据
            ClyjUserSearch clyjUserSearch = new ClyjUserSearch();
            clyjUserSearch.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjUserSearch.setSearchName(name);
            clyjUserSearch.setUserId(userId);
            clyjUserSearch.setSearchTime(new Date());
            clyjUserSearch.setFlag(0);
            bannerService.insert(clyjUserSearch);


            return ApiResult.success(list1);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return null;
    }

    /**
     * 搜索显示候选词
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSearchkey")
    public Map<String, Object> getSearchkey(@RequestBody JSONObject params) {
        Map<String, Object> result = new HashMap<String, Object>();
        String keyvalue = params.getString("keyvalue");
        try {
            if (keyvalue == null || keyvalue.equals("")) {
                return result;
            }
            result = this.bannerService.getSearchkey(keyvalue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 查询热门搜索
     *
     * @return
     */
    @RequestMapping("/getHotSearch")
    @ResponseBody
    public ApiResult getHotSearch(@RequestBody JSONObject params) {
        String flag = params.getString("flag");
        try {
            QueryWrapper<ClyjUserHotsearch> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("flag", Integer.parseInt(flag));
            queryWrapper.orderByDesc("number");
            List<ClyjUserHotsearch> clyjUserHotsearchList = this.clyjUserHotsearchMapper.selectList(queryWrapper);
            logger.debug("查询成功++++++++++++", clyjUserHotsearchList);
            return ApiResult.success(clyjUserHotsearchList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }
}

